package com.jocata.hibernate;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.transaction.Transaction;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
 
public class HibernateUtil {
 
    private static final SessionFactory sessionFactory = buildSessionFactory();
 
    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
//            return new Configuration().configure().buildSessionFactory();
           return new Configuration().configure().buildSessionFactory();
 
        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }
 
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    
    public static List<String> getCatalogList(){
		List<String> list = null;
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session session = sf.openSession();
		list=session.createQuery("select catalogName from Catalogs").list();
		session.close();
		return list;
		
	}
	
	public static HashMap<Integer, ArrayList<String>> getAliasListsByType(){
		List<Object[]> list = null;
		HashMap<Integer, ArrayList<String>> resultMap = new HashMap<Integer, ArrayList<String>>();
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session session = sf.openSession();
		String sql="select al.aliasTypeId,al.aliasTypeName from Alias as al";
		list=session.createQuery(sql).list();
		if (list == null || list.size() < 1)
			return null;
		Integer aliasTypeId = null;
		for (Object[] objArr : list) {
			aliasTypeId =(Integer) objArr[0];
		if (resultMap.containsKey(aliasTypeId)) {
			ArrayList<String> nameList = resultMap.get(aliasTypeId);
			nameList.add(objArr[1]+"");
			resultMap.put(aliasTypeId, nameList);
		} else {
			ArrayList<String> tempNameList = new ArrayList<String>();
			tempNameList.add(objArr[1]+"");
			resultMap.put(aliasTypeId, tempNameList);
		}
	}
    session.close();
	return resultMap;
	}
	
	public static List<String> getAllAliases(){
		List<String> list = null;
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session session = sf.openSession();
		list=session.createQuery("select al.aliasTypeName from Alias as al").list();
		session.close();
		return list;
	}
	public static List getCustomerPrincipalList(){
		List customerPrincipals = null;
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session session = sf.openSession();
		customerPrincipals = session.createQuery("from CustomerPrincipal").setMaxResults(1).list();
		session.close();
		return customerPrincipals;
	}
	public static List getWatchListNamesList(){
		List watchlstNames = null;
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session session = sf.openSession();
		watchlstNames = session.createQuery("from WatchlistName").list();
		session.close();
		return watchlstNames;
	}
	public static void saveMatchResult(MatchResult matchRes){
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session session = sf.openSession();
		org.hibernate.Transaction tx=session.beginTransaction();
		session.saveOrUpdate(matchRes);
		session.flush();
		tx.commit();
		
	}

}

